package com.fasttreatment.zzr.demo.fragment;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.android.androidlibrary.base.BaseFragment;
import com.fasttreatment.zzr.demo.R;
import com.fasttreatment.zzr.demo.adapter.GirlAdapter;
import com.fasttreatment.zzr.demo.model.GankEntity;
import com.fasttreatment.zzr.demo.net.JsonCallback;
import com.fasttreatment.zzr.demo.net.NetApi;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnItemClick;
import cn.droidlover.qtcontentlayout.QTContentLayout;
import cn.droidlover.xrecyclerview.RecyclerItemCallback;
import cn.droidlover.xrecyclerview.XRecyclerContentLayout;
import cn.droidlover.xrecyclerview.XRecyclerView;
import okhttp3.Call;

/**
 * Created by zzr on 17/1/11.
 */

public class Test1Fragment extends BaseFragment {
    @BindView(R.id.rc_contentlayou)
    XRecyclerContentLayout rcContentlayou;

    private GirlAdapter mGirlAdapter;
    protected static final int PAGE_SIZE = 10;
    protected static final int MAX_PAGE = 10;

    @Override
    public void initData(Bundle savedInstanceState) {
        initAdapter();
        loadData(1);
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_test1;
    }

    @Override
    public void setListener() {

    }

    public void initAdapter() {
        mGirlAdapter = new GirlAdapter(getContext());
        rcContentlayou.getRecyclerView().gridLayoutManager(getContext(), 2).setAdapter(mGirlAdapter);
        rcContentlayou.loadingView(View.inflate(getContext(), R.layout.view_loading, null));
        rcContentlayou.getRecyclerView().useDefLoadMoreView();
        mGirlAdapter.setRecItemClick(new RecyclerItemCallback<GankEntity.Item, GirlAdapter.ViewHolder>() {
            @Override
            public void onItemClick(int position, GankEntity.Item model, int tag, GirlAdapter.ViewHolder holder) {
                super.onItemClick(position, model, tag, holder);
                Toast.makeText(context, "position:" + position+"aaaaaaaaaa贼帅", Toast.LENGTH_SHORT).show();

            }
        });
        rcContentlayou.getRecyclerView().setOnRefreshAndLoadMoreListener(new XRecyclerView.OnRefreshAndLoadMoreListener() {
            @Override
            public void onRefresh() {
                loadData(1);
            }

            @Override
            public void onLoadMore(int page) {
                loadData(page);
            }
        });

    }

    public void loadData(final int page) {
        //showProgressDialog();
        NetApi.getGankData("福利", PAGE_SIZE, page, new JsonCallback<GankEntity>(1000) {
            @Override
            public void onFail(Call call, Exception e, int id) {
                //closeProgressDialog();
                rcContentlayou.setDisplayState(QTContentLayout.STATE_ERROR);
                Log.i("zzre",e+"");
            }

            @Override
            public void onResponse(GankEntity response, int id) {
                if (page > 1) {
                    mGirlAdapter.addData(response.getResults());
                } else {
                    mGirlAdapter.setData(response.getResults());
                }
                rcContentlayou.getRecyclerView().setPage(page, MAX_PAGE);
                if (mGirlAdapter.getItemCount() < 1) {
                    rcContentlayou.showEmpty();
                    return;
                }
                // closeProgressDialog();
            }

        });
    }

}
